<template>
  <div class="dashboard-container">
    <el-container>
      <el-header>
        <div class="header-content">
          <h1>欢迎使用系统</h1>
          <div class="user-info">
            <el-dropdown @command="handleCommand">
              <span class="el-dropdown-link">
                <el-icon><User /></el-icon>
                {{ userInfo.username }}
                <el-icon class="el-icon--right"><arrow-down /></el-icon>
              </span>
              <template #dropdown>
                <el-dropdown-menu>
                  <el-dropdown-item command="logout">退出登录</el-dropdown-item>
                </el-dropdown-menu>
              </template>
            </el-dropdown>
          </div>
        </div>
      </el-header>
      
      <el-main>
        <el-row :gutter="20">
          <el-col :span="24">
            <el-card>
              <template #header>
                <span>用户信息</span>
              </template>
              <div class="user-details">
                <p><strong>用户名:</strong> {{ userInfo.username }}</p>
                <p><strong>邮箱:</strong> {{ userInfo.email || '未设置' }}</p>
                <p><strong>登录时间:</strong> {{ loginTime }}</p>
              </div>
            </el-card>
          </el-col>
        </el-row>
        
        <el-row :gutter="20" style="margin-top: 20px;">
          <el-col :span="8">
            <el-card>
              <el-statistic title="在线用户" :value="123" />
            </el-card>
          </el-col>
          <el-col :span="8">
            <el-card>
              <el-statistic title="今日访问" :value="456" />
            </el-card>
          </el-col>
          <el-col :span="8">
            <el-card>
              <el-statistic title="系统消息" :value="78" />
            </el-card>
          </el-col>
        </el-row>
      </el-main>
    </el-container>
  </div>
</template>

<script>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage, ElMessageBox } from 'element-plus'
import { User, ArrowDown } from '@element-plus/icons-vue'

export default {
  name: 'Dashboard',
  components: {
    User,
    ArrowDown
  },
  setup() {
    const router = useRouter()
    const userInfo = ref({})
    const loginTime = ref('')
    
    onMounted(() => {
      const storedUserInfo = localStorage.getItem('userInfo')
      if (storedUserInfo) {
        userInfo.value = JSON.parse(storedUserInfo)
      }
      loginTime.value = new Date().toLocaleString()
    })
    
    const handleCommand = async (command) => {
      if (command === 'logout') {
        try {
          await ElMessageBox.confirm('确定要退出登录吗?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          })
          
          localStorage.removeItem('token')
          localStorage.removeItem('userInfo')
          ElMessage.success('退出成功')
          router.push('/login')
        } catch (error) {
          // User cancelled
        }
      }
    }
    
    return {
      userInfo,
      loginTime,
      handleCommand
    }
  }
}
</script>

<style scoped>
.dashboard-container {
  min-height: 100vh;
}

.el-header {
  background-color: #545c64;
  color: #fff;
  line-height: 60px;
}

.header-content {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.header-content h1 {
  margin: 0;
  font-size: 20px;
}

.user-info .el-dropdown-link {
  color: #fff;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 5px;
}

.el-main {
  background-color: #f0f2f5;
  padding: 20px;
}

.user-details p {
  margin: 10px 0;
  font-size: 14px;
}
</style>